package com.qs.javame.thread.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * 测试2：执行wait()操作时，线程被中断，抛出InterruptedException异常，标识位会被清除，isInterrupted()返回false
 */
public class TestInterrupt3 {
    public static void main(String[] args) {
        Thread  thread = new Thread(new Task());
        thread.start();
        thread.interrupt();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("main线程执行完了~");

    }

    static class Task implements Runnable{

        @Override
        public synchronized void run() {
            //线程调用wait()处于等待状态时，线程中断时抛出InterruptedException异常，标识位会被清除，isInterrupted()返回false。
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Thread currentThread = Thread.currentThread();
            //返回false
            System.out.println(currentThread.getName() + "-线程中断：" + currentThread.isInterrupted());
        }
    }
}


